//---------------Sonic CD Special Setup Script----------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases

// Universal
private alias Object.Value5 : Object.ScreenDepth

// HUD Alias
private alias Object.Value0 : HUD.UFOsCount
private alias Object.Value1 : HUD.TimeSeconds
private alias Object.Value2 : HUD.TimeFrames
private alias Object.Value6 : HUD.BonusUFONo // "No" akin to Entity No, not as in "is there no UFO?"
private alias Object.Value7 : Checkpoint.DontRun


// UFO Alias
private alias Object.YPos : Object.ZPos

// SSSonic Aliases
private alias object.controlmode : SSSonic.ControlMode

// ControlMode Aliases
private alias -1 : CONTROLMODE_NONE

// Stage Finish State Aliases
private alias 9 : STAGEFINISH_LOADLEVELSELECT

// Game Mode Aliases
private alias 2 : MODE_TIMEATTACK

// Stage SFX
private alias 5 : SFX_S_TIMEUFO
private alias 7 : SFX_S_DROWNALERT

// Soundtrack
private alias 0 : OST_JP

// Priority
private alias 1 : PRIORITY_ACTIVE

// Tile Layer Types
private alias 4 : LAYER_3DSKY

public value MBZSetup_value31 = 0;
public value MBZSetup_value30 = 0;
public value MBZSetup_value29 = 0;
public value MBZSetup_value28 = 0;

public value ThunderActive = 0;
//public value Crafting.State = 0;

public value Delta_HP = 90;
public value Delta_YourTurn = 1;

public value Delta_SlenderHP = 100;
public value Delta_SlenderMercy = 0;

public value Delta_BecomeGreen = 0;

public value Delta_ShowMercy = 0;

public value Delta_Defending = 0;
public value Delta_Attacker = 0;		//0	= S;	1 = F

public value Delta_AttackHardMode = 0;
public value PlayerDied = 0;
public value SlenderSpared = 0;
public value Delta_Activate = 0;

public alias saveRAM[6905] : saved.SlenderOST


event ObjectMain
	if object[20].type == TypeName[Blank Object]
		if Checkpoint.DontRun == 0
			if lampPostID == 1
				Object[4].Value0 = 0
				Object[6].state = 1
			end if		
			Checkpoint.DontRun = 1
		end if
	end if

	scene3D.vertexCount = 0
	scene3D.faceCount = 0
	
	// Update the oscillation value
	// Although it's normally used for synced movements throughout a stage, the Oscillation value is never actually used within the Special Stage
	Oscillation++
	if Oscillation == 512
		Oscillation = 0
	end if

	CheckCurrentStageFolder("SS1")
	if checkResult == 1

		MBZSetup_value31++
		
		if MBZSetup_value31 == MBZSetup_value30 
			MBZSetup_value29 = 16
			if Object[8].state < 6
				PlaySfx(SfxName[SlenderThunder], 0)
			end if
				MBZSetup_value31 = 0		
				Rand(MBZSetup_value30, 1000)
				MBZSetup_value30 += 200
		end if
			
		temp1 = 0
		if MBZSetup_value29 > 0
			MBZSetup_value29--
			temp0 = MBZSetup_value29
			temp0 /= 4
			temp0 %= 2
			if temp0 == 0
				temp1 = 1
			end if
		end if
		
		if temp1 == 1
			//lightning colours
			CopyPalette(0, 154, 0, 208, 4)
			ThunderActive = 1
		else
			//regular colours
			CopyPalette(0, 213, 0, 208, 4)
			ThunderActive = 0
		end if
	end if
end event


event ObjectDraw
	// Sort the drawing orders of all entities
	// (Things further away from the camera should be drawn first and things closest should be last, etc.)
	
	temp0 = 0
	temp1 = Screen.DrawListSize[3]
	while temp0 < temp1
		temp2 = temp1
		temp2--
		
		while temp2 > temp0
			temp3 = temp2
			temp2--
			
			// Get the object slots of the two objects
			GetDrawListEntityRef(ArrayPos0, 3, temp3)
			GetDrawListEntityRef(ArrayPos1, 3, temp2)
			
			// Compare their sorted draw orders
			if Object[ArrayPos0].ScreenDepth > Object[ArrayPos1].ScreenDepth
				// Swap them around if needed
				SetDrawListEntityRef(ArrayPos0, 3, temp2)
				SetDrawListEntityRef(ArrayPos1, 3, temp3)
			end if
		loop
		
		temp0++
	loop

	Draw3DScene()

end event


event ObjectStartup
	// Despite not having any SpriteFrames to draw with, go ahead and load the Special Stage objects sheet anyway
	// (It doesn't make much of a difference but hey, may as well anyway!)
	LoadSpriteSheet("3D_Test/Objects.gif")
	
	// Set the ground to the "3d Sky" layer type
	// -> This way, it'll render in high-quality mode if possible, as opposed to the "3d Floor" type
	TileLayer[0].Type = 3
	
	ThunderActive = 0
	

	SetMusicTrack("ambience.ogg", 0, 1)
	SetMusicTrack("RobloxHorror.ogg", 1, false)
	SetMusicTrack("ThisIsA.ogg", 2, 1)
	if saved.SlenderOST == 0	//cast
		SetMusicTrack("BossIntro.ogg", 3, 1)
		SetMusicTrack("ForeverTrouble.ogg", 4, 376200)
	else
		SetMusicTrack("BossIntroM.ogg", 3, 1)
		SetMusicTrack("ForeverTroubleM.ogg", 4, 376200)
	end if
	SetMusicTrack("ForeverLose.ogg", 5, 0)
	SetMusicTrack("victory.ogg", 6, 0)
	SetMusicTrack("ForeverClutch.ogg", 9, 1)	


	Stage.PauseEnabled = false
	
	// Origins changed this up a bit - before, Time would always be disabled, but Origins makes it enabled for standard play
	if Options.GameMode == MODE_TIMEATTACK
		Stage.TimeEnabled = false
	else
		Stage.TimeEnabled = true
	end if
	
	Object[23].Type = TypeName[Special Setup]
	Object[23].Priority = PRIORITY_ACTIVE
	

	Rand(MBZSetup_value30, 1000)
	MBZSetup_value30 += 200

	// Give it a draw order lower right before the primary one, to make sure it can sort all the upcoming 3d entities correctly
	Object[23].DrawOrder = 2
	
	// Cycle through all Blank Objects?
	// There was likely a proper name here in the original source,
	// but it was an invalid name so upon compiling, it got resolved as 0
	// What this original name was though, we have no idea...
	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[Blank Object]
			// If the object is blank... then blank it again...
			ResetObjectEntity(ArrayPos0, TypeName[Blank Object], 0, 0, 0)
		end if
		
		ArrayPos0++
	loop
	
	scene3D.projectionX = 255
	scene3D.projectionY = 255
	scene3D.fogStrength = 255
	scene3D.fogColor = 0
	
end event


// ========================
// Editor events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143) // "Script" Box

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
